package se.webbzon.boltzmann.menu;

import java.awt.Color;
import java.awt.Font;

import se.webbzon.oschi01.font.HorizontalAlignment;
import se.webbzon.oschi01.font.VerticalAlignment;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;

public class MText extends MItem {
	
	// The layout of the text in this menu text
	private final TextLayout layout;
	
	// Whatever the shadow is active or not
	private boolean shadow;
	
	// The color of the text
	private Color color;
	
	// The alignment of the text in this menu text
	private HorizontalAlignment hAlign;
	private VerticalAlignment vAlign;
	
	// The text of this menu text
	private String[] lines;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public MText(TextLayout layout, String text) {
		this(layout,0,text);
	}
	
	public MText(TextLayout layout, int depth, String text) {
		super(depth);
		this.layout = layout;
		color = layout.getColor();
		hAlign = HorizontalAlignment.LEFT;
		vAlign = VerticalAlignment.TOP;
		shadow = false;
		setText(text);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns true if the text has a shadow. **/
	public boolean hasShadow() {
		return shadow;
	}

	/** Sets whatever the text has a shadow. **/
	public void setShadow(boolean shadow) {
		this.shadow = shadow;
	}
	
	/** Returns the color of this menu text. **/
	public Color getColor() {
		return color;
	}

	/** Sets the color of this menu text. If the provided color is
	 * null then the default color of the layout is used. **/
	public void setColor(Color color) {
		if (color == null)
			this.color = layout.getColor();
		else
			this.color = color;
	}
	
	/** Returns the horizontal alignment of the text. **/
	public HorizontalAlignment getHAlignment() {
		return hAlign;
	}

	/** Sets the horizontal alignment of the text. **/
	public void setHAlignment(HorizontalAlignment hAlign) {
		this.hAlign = hAlign;
	}

	/** Returns the vertical alignment of the text. **/
	public VerticalAlignment getVAlignment() {
		return vAlign;
	}

	/** Sets the vertical alignment of the text. **/
	public void setVAlignment(VerticalAlignment vAlign) {
		this.vAlign = vAlign;
	}
	
	/** Sets the text of this menu text. **/
	public void setText(String text) {
		lines = text.split("\n");
	}

	/** Returns the text of this menu text. **/
	public String getText() {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < lines.length; i++) {
			if (i == 0)
				builder.append(lines[0]);
			else
				builder.append("\n" + lines[i]);
		}
		return builder.toString();
	}

	/** Draws the text of this menu text onto the provided pane. **/
	public void draw(WorldCameraPane hud, int x, int y) {
		final Font font = layout.getFont();
		hud.setFont(font);
		hud.setVAlignment(vAlign);
		hud.setHAlignment(hAlign);
		int dy = font.getSize();
		y -= dy * (lines.length-1) / 2; 
		if (shadow)
			draw(hud,Color.black,x+1,y+1,dy);
		draw(hud,color,x,y,dy);	
	}
	
	@Override public void onDraw(GameMenu menu, WorldCameraPane hud) {
		draw(hud,getX(),getY());
	}

	@Override public void onPress(GameMenu menu, int x, int y) {};

	@Override public void onRelease(GameMenu menu, int x, int y) {};

	@Override public boolean isDraggable() {
		return false;
	}

	@Override public boolean isFocusable() {
		return false;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Draws all the line of the text onto the camera pane starting
	 * at (x,y) and increments the y position by dy for each row. **/
	private final void draw(WorldCameraPane hud, Color color, int x, int y, int dy) {
		hud.setColor(color);
		for (int i = 0; i < lines.length; i++) {
			hud.drawString(lines[i], x, y);
			y += dy;
		}
	}

}
